3 * Helper for migrating actors from pre-1.31 columns to the 'actor' table
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 use Wikimedia\Rdbms\IDatabase
;
26 require_once __DIR__
. '/../Maintenance.php';
29 * Maintenance script that migrates actors from pre-1.31 columns to the
32 * @ingroup Maintenance
34 class MigrateActors
extends LoggedUpdateMaintenance
{
35 public function __construct() {
36 parent
::__construct();
37 $this->addDescription( 'Migrates actors from pre-1.31 columns to the \'actor\' table' );
38 $this->setBatchSize( 100 );
41 protected function getUpdateKey() {
45 protected function doDBUpdates() {
46 global $wgActorTableSchemaMigrationStage;
48 if ( !( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) ) {
50 "...cannot update while \$wgActorTableSchemaMigrationStage lacks SCHEMA_COMPAT_WRITE_NEW\n"
55 $this->output( "Creating actor entries for all registered users\n" );
57 $dbw = $this->getDB( DB_MASTER
);
58 $max = $dbw->selectField( 'user', 'MAX(user_id)', '', __METHOD__
);
60 while ( $end < $max ) {
62 $end = min( $start +
$this->mBatchSize
, $max );
63 $this->output( "... $start - $end\n" );
67 [ 'actor_user' => 'user_id', 'actor_name' => 'user_name' ],
68 [ "user_id >= $start", "user_id <= $end" ],
71 [ 'ORDER BY' => [ 'user_id' ] ]
73 $count +
= $dbw->affectedRows();
76 $this->output( "Completed actor creation, added $count new actor(s)\n" );
79 $errors +
= $this->migrateToTemp(
80 'revision', 'rev_id', [ 'revactor_timestamp' => 'rev_timestamp', 'revactor_page' => 'rev_page' ],
81 'rev_user', 'rev_user_text', 'revactor_rev', 'revactor_actor'
83 $errors +
= $this->migrate( 'archive', 'ar_id', 'ar_user', 'ar_user_text', 'ar_actor' );
84 $errors +
= $this->migrate( 'ipblocks', 'ipb_id', 'ipb_by', 'ipb_by_text', 'ipb_by_actor' );
85 $errors +
= $this->migrate( 'image', 'img_name', 'img_user', 'img_user_text', 'img_actor' );
86 $errors +
= $this->migrate(
87 'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_user', 'oi_user_text', 'oi_actor'
89 $errors +
= $this->migrate( 'filearchive', 'fa_id', 'fa_user', 'fa_user_text', 'fa_actor' );
90 $errors +
= $this->migrate( 'recentchanges', 'rc_id', 'rc_user', 'rc_user_text', 'rc_actor' );
91 $errors +
= $this->migrate( 'logging', 'log_id', 'log_user', 'log_user_text', 'log_actor' );
93 $errors +
= $this->migrateLogSearch();
99 * Calculate a "next" condition and a display string
100 * @param IDatabase $dbw
101 * @param string[] $primaryKey Primary key of the table.
102 * @param object $row Database row
103 * @return array [ string $next, string $display ]
105 private function makeNextCond( $dbw, $primaryKey, $row ) {
108 for ( $i = count( $primaryKey ) - 1; $i >= 0; $i-- ) {
109 $field = $primaryKey[$i];
110 $display[] = $field . '=' . $row->$field;
111 $value = $dbw->addQuotes( $row->$field );
112 if ( $next === '' ) {
113 $next = "$field > $value";
115 $next = "$field > $value OR $field = $value AND ($next)";
118 $display = implode( ' ', array_reverse( $display ) );
119 return [ $next, $display ];
123 * Make the subqueries for `actor_id`
124 * @param IDatabase $dbw
125 * @param string $userField User ID field name
126 * @param string $nameField User name field name
127 * @return string SQL fragment
129 private function makeActorIdSubquery( $dbw, $userField, $nameField ) {
130 $idSubquery = $dbw->buildSelectSubquery(
133 [ "$userField = actor_user" ],
136 $nameSubquery = $dbw->buildSelectSubquery(
139 [ "$nameField = actor_name" ],
142 return "CASE WHEN $userField = 0 OR $userField IS NULL THEN $nameSubquery ELSE $idSubquery END";
146 * Add actors for anons in a set of rows
147 * @param IDatabase $dbw
148 * @param string $nameField
149 * @param object[] &$rows
150 * @param array &$complainedAboutUsers
151 * @param int &$countErrors
152 * @return int Count of actors inserted
154 private function addActorsForRows(
155 IDatabase
$dbw, $nameField, array &$rows, array &$complainedAboutUsers, &$countErrors
161 foreach ( $rows as $index => $row ) {
162 $keep[$index] = true;
163 if ( $row->actor_id
=== null ) {
164 // All registered users should have an actor_id already. So
165 // if we have a usable name here, it means they didn't run
166 // maintenance/cleanupUsersWithNoId.php
167 $name = $row->$nameField;
168 if ( User
::isUsableName( $name ) ) {
169 if ( !isset( $complainedAboutUsers[$name] ) ) {
170 $complainedAboutUsers[$name] = true;
172 "User name \"$name\" is usable, cannot create an anonymous actor for it."
173 . " Run maintenance/cleanupUsersWithNoId.php to fix this situation.\n"
176 unset( $keep[$index] );
179 $needActors[$name] = 0;
183 $rows = array_intersect_key( $rows, $keep );
188 array_map( function ( $v ) {
192 }, array_keys( $needActors ) ),
195 $countActors +
= $dbw->affectedRows();
199 [ 'actor_id', 'actor_name' ],
200 [ 'actor_name' => array_keys( $needActors ) ],
203 foreach ( $res as $row ) {
204 $needActors[$row->actor_name
] = $row->actor_id
;
206 foreach ( $rows as $row ) {
207 if ( $row->actor_id
=== null ) {
208 $row->actor_id
= $needActors[$row->$nameField];
217 * Migrate actors in a table.
219 * Assumes any row with the actor field non-zero have already been migrated.
220 * Blanks the name field when migrating.
222 * @param string $table Table to migrate
223 * @param string|string[] $primaryKey Primary key of the table.
224 * @param string $userField User ID field name
225 * @param string $nameField User name field name
226 * @param string $actorField Actor field name
227 * @return int Number of errors
229 protected function migrate( $table, $primaryKey, $userField, $nameField, $actorField ) {
230 $complainedAboutUsers = [];
232 $primaryKey = (array)$primaryKey;
233 $pkFilter = array_flip( $primaryKey );
235 "Beginning migration of $table.$userField and $table.$nameField to $table.$actorField\n"
239 $dbw = $this->getDB( DB_MASTER
);
240 $actorIdSubquery = $this->makeActorIdSubquery( $dbw, $userField, $nameField );
246 // Fetch the rows needing update
249 array_merge( $primaryKey, [ $userField, $nameField, 'actor_id' => $actorIdSubquery ] ),
256 'ORDER BY' => $primaryKey,
257 'LIMIT' => $this->mBatchSize
,
260 if ( !$res->numRows() ) {
264 // Insert new actors for rows that need one
265 $rows = iterator_to_array( $res );
266 $lastRow = end( $rows );
267 $countActors +
= $this->addActorsForRows(
268 $dbw, $nameField, $rows, $complainedAboutUsers, $countErrors
271 // Update the existing rows
272 foreach ( $rows as $row ) {
273 if ( !$row->actor_id
) {
274 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
276 "Could not make actor for row with $display "
277 . "$userField={$row->$userField} $nameField={$row->$nameField}\n"
285 $actorField => $row->actor_id
,
287 array_intersect_key( (array)$row, $pkFilter ) +
[
292 $countUpdated +
= $dbw->affectedRows();
295 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
296 $this->output( "... $display\n" );
301 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
302 . "$countErrors error(s)\n"
308 * Migrate actors in a table to a temporary table.
310 * Assumes the new table is named "{$table}_actor_temp", and it has two
311 * columns, in order, being the primary key of the original table and the
313 * Blanks the name field when migrating.
315 * @param string $table Table to migrate
316 * @param string $primaryKey Primary key of the table.
317 * @param array $extra Extra fields to copy
318 * @param string $userField User ID field name
319 * @param string $nameField User name field name
320 * @param string $newPrimaryKey Primary key of the new table.
321 * @param string $actorField Actor field name
323 protected function migrateToTemp(
324 $table, $primaryKey, $extra, $userField, $nameField, $newPrimaryKey, $actorField
326 $complainedAboutUsers = [];
328 $newTable = $table . '_actor_temp';
330 "Beginning migration of $table.$userField and $table.$nameField to $newTable.$actorField\n"
334 $dbw = $this->getDB( DB_MASTER
);
335 $actorIdSubquery = $this->makeActorIdSubquery( $dbw, $userField, $nameField );
341 // Fetch the rows needing update
343 [ $table, $newTable ],
344 [ $primaryKey, $userField, $nameField, 'actor_id' => $actorIdSubquery ] +
$extra,
345 [ $newPrimaryKey => null ] +
$next,
348 'ORDER BY' => $primaryKey,
349 'LIMIT' => $this->mBatchSize
,
352 $newTable => [ 'LEFT JOIN', "{$primaryKey}={$newPrimaryKey}" ],
355 if ( !$res->numRows() ) {
359 // Insert new actors for rows that need one
360 $rows = iterator_to_array( $res );
361 $lastRow = end( $rows );
362 $countActors +
= $this->addActorsForRows(
363 $dbw, $nameField, $rows, $complainedAboutUsers, $countErrors
370 foreach ( $rows as $row ) {
371 if ( !$row->actor_id
) {
372 list( , $display ) = $this->makeNextCond( $dbw, [ $primaryKey ], $row );
374 "Could not make actor for row with $display "
375 . "$userField={$row->$userField} $nameField={$row->$nameField}\n"
381 $newPrimaryKey => $row->$primaryKey,
382 $actorField => $row->actor_id
,
384 foreach ( $extra as $to => $from ) {
385 $ins[$to] = $row->$to; // It's aliased
388 $updates[] = $row->$primaryKey;
390 $this->beginTransaction( $dbw, __METHOD__
);
391 $dbw->insert( $newTable, $inserts, __METHOD__
);
392 $countUpdated +
= $dbw->affectedRows();
393 $this->commitTransaction( $dbw, __METHOD__
);
396 // Calculate the "next" condition
397 list( $n, $display ) = $this->makeNextCond( $dbw, [ $primaryKey ], $lastRow );
399 $this->output( "... $display\n" );
403 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
404 . "$countErrors error(s)\n"
410 * Migrate actors in the log_search table.
411 * @return int Number of errors
413 protected function migrateLogSearch() {
414 $complainedAboutUsers = [];
416 $primaryKey = [ 'ls_value', 'ls_log_id' ];
417 $this->output( "Beginning migration of log_search\n" );
420 $dbw = $this->getDB( DB_MASTER
);
427 // Fetch the rows needing update
429 [ 'log_search', 'actor' ],
430 [ 'ls_value', 'ls_log_id', 'actor_id' ],
432 'ls_field' => 'target_author_id',
437 'ORDER BY' => $primaryKey,
438 'LIMIT' => $this->mBatchSize
,
440 [ 'actor' => [ 'LEFT JOIN', 'ls_value = ' . $dbw->buildStringCast( 'actor_user' ) ] ]
442 if ( !$res->numRows() ) {
446 // Insert a 'target_author_actor' for each 'target_author_id'
448 foreach ( $res as $row ) {
450 if ( !$row->actor_id
) {
451 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
452 $this->error( "No actor for target_author_id row with $display\n" );
457 'ls_field' => 'target_author_actor',
458 'ls_value' => $row->actor_id
,
459 'ls_log_id' => $row->ls_log_id
,
462 $dbw->insert( 'log_search', $ins, __METHOD__
, [ 'IGNORE' ] );
463 $countInserted +
= $dbw->affectedRows();
465 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
466 $this->output( "... target_author_id, $display\n" );
472 // Fetch the rows needing update
474 [ 'log_search', 'actor' ],
475 [ 'ls_value', 'ls_log_id', 'actor_id' ],
477 'ls_field' => 'target_author_ip',
482 'ORDER BY' => $primaryKey,
483 'LIMIT' => $this->mBatchSize
,
485 [ 'actor' => [ 'LEFT JOIN', 'ls_value = actor_name' ] ]
487 if ( !$res->numRows() ) {
491 // Insert new actors for rows that need one
492 $rows = iterator_to_array( $res );
493 $lastRow = end( $rows );
494 $countActors +
= $this->addActorsForRows(
495 $dbw, 'ls_value', $rows, $complainedAboutUsers, $countErrors
498 // Insert a 'target_author_actor' for each 'target_author_ip'
500 foreach ( $rows as $row ) {
501 if ( !$row->actor_id
) {
502 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
503 $this->error( "Could not make actor for target_author_ip row with $display\n" );
508 'ls_field' => 'target_author_actor',
509 'ls_value' => $row->actor_id
,
510 'ls_log_id' => $row->ls_log_id
,
513 $dbw->insert( 'log_search', $ins, __METHOD__
, [ 'IGNORE' ] );
514 $countInserted +
= $dbw->affectedRows();
516 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
517 $this->output( "... target_author_ip, $display\n" );
522 "Completed migration, inserted $countInserted row(s) with $countActors new actor(s), "
523 . "$countErrors error(s)\n"